**************************************************************************************************
*** This do file creates the replication results for 
*** The ties that bind: The role of migrants in the uneven geography of international telephone traffic							 	*/
*** Richard Perkins (LSE)																		*/
*** Eric Neumayer (LSE)																			*/
*** 																							*/
*** Published in: Global Networks, 2012															*/
**************************************************************************************************
**************************************************************************************************
/* Note: 
You have to change "local DIR" to the directory you copy the original stata files contained 	*/
/* in the zip file and then run the do file. 													*/
**************************************************************************************************

version 11.0
drop _all
clear matrix
clear mata
set mem 800m
set mat 5000

capture net install outreg2, from(http://fmwww.bc.edu/RePEc/bocode/o)			/* checks whether outreg2 is installed 		*/

***********************************************************************************
local DIR = "C:\Research\Other articles\Telecommunication"  /*change relative path to the directory where the files are located */
cd "`DIR'"
***********************************************************************************

use "Article for Global Networks (telephony).dta", clear

xi: reg   lntrafvolumemill_av  lntrade lnfdi lnvisitors lnmigrantstock  contiguity4dummy comlang  lndistance   coloniallinkplusrussia    lngdppcconst_sumboth  lnpop_sumboth lnpop_sumboth_sq   i.reporter*i.year i.partner*i.year   if year>2000, robust cluster(dyadid)
outreg2  using table1, replace excel
estat ic

* interaction with distance
xi: reg   lntrafvolumemill_av  lnmigrantstock lndistance  lnmigrant_lndistance lntrade lnfdi lnvisitors   contiguity4dummy comlang   coloniallinkplusrussia     lngdppcconst_sumboth  lnpop_sumboth lnpop_sumboth_sq    i.reporter*i.year i.partner*i.year   if year>2000, robust cluster(dyadid)
estat ic
outreg2  using table1, append excel
#delimit ;
capture drop MV conb conse a upper lower;
generate MV=((_n-1)/1);

replace  MV=. if _n>12;

*     ****************************************************************  *;
*       Grab elements of the coefficient and variance-covariance matrix *;
*       that are required to calculate the marginal effect and standard *;
*       errors.                                                         *;
*     ****************************************************************  *;

matrix b=e(b);
matrix list b; 
matrix V=e(V);
matrix list V;
 
scalar b1=b[1,1]; 
scalar b2=b[1,2];
scalar b3=b[1,3];


scalar varb1=V[1,1]; 
scalar varb2=V[2,2]; 
scalar varb3=V[3,3];


scalar covb1b3=V[1,3]; 
scalar covb2b3=V[2,3];

scalar list b1 b2 b3 varb1 varb2 varb3 covb1b3 covb2b3;


*     ****************************************************************  *;
*       Calculate the marginal effect of X on Y for all MV values of    *;
*       the modifying variable Z.                                       *;
*     ****************************************************************  *;

gen conb=b1+b3*MV if _n<12;


*     ****************************************************************  *;
*       Calculate the standard errors for the marginal effect of X on Y *;
*       for all MV values of the modifying variable Z.                  *;
*     ****************************************************************  *;

gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) if _n<12; 


*     ****************************************************************  *;
*       Generate upper and lower bounds of the confidence interval.     *;
*       Specify the significance of the confidence interval.            *;
*     ****************************************************************  *;

gen a=1.64*conse;
 
gen upper=conb+a;
 
gen lower=conb-a;

*     ****************************************************************  *;
*       Graph the marginal effect of X on Y across the desired range of *;
*       the modifying variable Z.  Show the confidence interval.        *;
*     ****************************************************************  *;

graph twoway line conb   MV, clwidth(medium) clcolor(blue) clcolor(black)
        ||   line upper  MV, clpattern(dash) clwidth(thin) clcolor(black)
        ||   line lower  MV, clpattern(dash) clwidth(thin) clcolor(black)
        ||   ,   
             xlabel(0 1 2 3 4 5 6 7 8 9 10, labsize(2.5)) 
             ylabel(0 .1 .2 .3 .4,   labsize(2.5))
             yscale(noline)
             xscale(noline)
             legend(col(1) order(1 2) label(1 "Marginal effect of migrant stocks") 
                                      label(2 "90% Confidence interval") 
                                      label(3 " "))
             yline(0, lcolor(black))   
             xtitle( "ln(distance)", size(3)  )
             xsca(titlegap(2))
             ysca(titlegap(2))
             scheme(s2mono) graphregion(fcolor(white));
             
*     ****************************************************************  *;
*                 Figure can be saved in a variety of formats.          *;
*     ****************************************************************  *; 

graph export  figure1.eps, replace;
#delimit cr


* interaction with sum of GDP
xi: reg   lntrafvolumemill_av  lnmigrantstock  lngdppcconst_sumboth   lnmigrant_lngdppc_sumboth lntrade lnfdi lnvisitors   contiguity4dummy comlang  lndistance  coloniallinkplusrussia     lnpop_sumboth lnpop_sumboth_sq    i.reporter*i.year i.partner*i.year   if year>2000, robust cluster(dyadid)
estat ic
outreg2  using table1, append excel
#delimit ;
capture drop MV conb conse a upper lower;
generate MV=((_n-1)/1)+5;

replace  MV=. if _n>9;

*     ****************************************************************  *;
*       Grab elements of the coefficient and variance-covariance matrix *;
*       that are required to calculate the marginal effect and standard *;
*       errors.                                                         *;
*     ****************************************************************  *;

matrix b=e(b);
matrix list b; 
matrix V=e(V);
matrix list V;
 
scalar b1=b[1,1]; 
scalar b2=b[1,2];
scalar b3=b[1,3];


scalar varb1=V[1,1]; 
scalar varb2=V[2,2]; 
scalar varb3=V[3,3];


scalar covb1b3=V[1,3]; 
scalar covb2b3=V[2,3];

scalar list b1 b2 b3 varb1 varb2 varb3 covb1b3 covb2b3;


*     ****************************************************************  *;
*       Calculate the marginal effect of X on Y for all MV values of    *;
*       the modifying variable Z.                                       *;
*     ****************************************************************  *;

gen conb=b1+b3*MV if _n<9;


*     ****************************************************************  *;
*       Calculate the standard errors for the marginal effect of X on Y *;
*       for all MV values of the modifying variable Z.                  *;
*     ****************************************************************  *;

gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) if _n<9; 


*     ****************************************************************  *;
*       Generate upper and lower bounds of the confidence interval.     *;
*       Specify the significance of the confidence interval.            *;
*     ****************************************************************  *;

gen a=1.64*conse;
 
gen upper=conb+a;
 
gen lower=conb-a;

*     ****************************************************************  *;
*       Graph the marginal effect of X on Y across the desired range of *;
*       the modifying variable Z.  Show the confidence interval.        *;
*     ****************************************************************  *;

graph twoway line conb   MV, clwidth(medium) clcolor(blue) clcolor(black)
        ||   line upper  MV, clpattern(dash) clwidth(thin) clcolor(black)
        ||   line lower  MV, clpattern(dash) clwidth(thin) clcolor(black)
        ||   ,   
             xlabel(5 6 7 8 9 10 11 12, labsize(2.5)) 
             ylabel(0 .1 .2 .3 .4 .5,   labsize(2.5))
             yscale(noline)
             xscale(noline)
             legend(col(1) order(1 2) label(1 "Marginal effect of migrant stocks") 
                                      label(2 "90% Confidence interval") 
                                      label(3 " "))
             yline(0, lcolor(black))   
             xtitle( "ln(sum of GDP pc in both countries)", size(3)  )
             xsca(titlegap(2))
             ysca(titlegap(2))
             scheme(s2mono) graphregion(fcolor(white));
             
*     ****************************************************************  *;
*                 Figure can be saved in a variety of formats.          *;
*     ****************************************************************  *; 

graph export  figure2.eps, replace;
#delimit cr


** Comparing AIC/BIC for model fit purposes, throwing out one relational variable at a time
* Without migrant stock
xi: reg   lntrafvolumemill_av  lntrade lnfdi lnvisitors   contiguity4dummy comlang  lndistance   coloniallinkplusrussia    lngdppcconst_sumboth  lnpop_sumboth lnpop_sumboth_sq   i.reporter*i.year i.partner*i.year   if year>2000, robust cluster(dyadid)
estat ic
* Without visitors
xi: reg   lntrafvolumemill_av  lntrade lnfdi  lnmigrantstock  contiguity4dummy comlang  lndistance   coloniallinkplusrussia    lngdppcconst_sumboth  lnpop_sumboth lnpop_sumboth_sq   i.reporter*i.year i.partner*i.year   if year>2000, robust cluster(dyadid)
estat ic
* Without FDI
xi: reg   lntrafvolumemill_av  lntrade lnvisitors lnmigrantstock  contiguity4dummy comlang  lndistance   coloniallinkplusrussia    lngdppcconst_sumboth  lnpop_sumboth lnpop_sumboth_sq   i.reporter*i.year i.partner*i.year   if year>2000, robust cluster(dyadid)
estat ic
* Without trade
xi: reg   lntrafvolumemill_av  lnfdi lnvisitors lnmigrantstock  contiguity4dummy comlang  lndistance   coloniallinkplusrussia    lngdppcconst_sumboth  lnpop_sumboth lnpop_sumboth_sq   i.reporter*i.year i.partner*i.year   if year>2000, robust cluster(dyadid)
estat ic
